package leetcode.tree;

import com.sun.corba.se.spi.ior.iiop.IIOPFactories;

import java.util.Arrays;

/**
 * @author zhangjunfei
 * @date 2023/8/24 17:18
 */
public class L_654 {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        if (nums == null || nums.length <= 0) {
            return null;
        }
        int index = 0;
        int max = nums[0];

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }

        TreeNode root = new TreeNode(max);
        root.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums, index, nums.length));
        root.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums, 0, index));
        return root;
    }
}
